package nc.bs.ic.ic209;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import javax.naming.NamingException;
import nc.bs.ic.pub.bill.BcurrArithDMO;
import nc.bs.ic.pub.bill.GeneralBillDMO;
import nc.bs.ic.pub.bill.GeneralSqlString;
import nc.bs.ic.pub.settlement.SettlementDMO;
import nc.bs.ml.NCLangResOnserver;
import nc.bs.pub.SystemException;
import nc.vo.ic.pub.bill.GeneralBillHeaderVO;
import nc.vo.ic.pub.bill.GeneralBillItemVO;
import nc.vo.ic.pub.bill.GeneralBillVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.pub.lang.UFDate;
import nc.vo.pub.lang.UFDouble;
import nc.vo.to.pub.BillHeaderVO;
import nc.vo.to.pub.BillItemVO;
import nc.vo.to.pub.BillVO;

public class GeneralHDMO extends GeneralBillDMO
{
  public GeneralHDMO()
    throws NamingException, SystemException
  {
  }

  public GeneralHDMO(String dbName)
    throws NamingException, SystemException
  {
    super(dbName);
  }

  public void checkWorkFlow(GeneralBillVO voBill)
    throws BusinessException
  {
    if ((voBill == null) || (voBill.getParentVO() == null) || (voBill.getChildrenVO() == null) || (voBill.getChildrenVO().length == 0) || (!voBill.isHaveSourceBill()))
    {
      return;
    }

    String cbilltype = voBill.getHeaderVO().getCbilltypecode();
    ArrayList alcode = new ArrayList();

    GeneralBillItemVO[] voaItem = (GeneralBillItemVO[])(GeneralBillItemVO[])voBill.getChildrenVO();
    ArrayList alFirstBid = new ArrayList();
    String cfirstbid = null;
    int size = voaItem.length;
    for (int i = 0; i < size; ++i) {
      cfirstbid = voaItem[i].getCfirstbillbid();
      if ((cfirstbid != null) && (!alFirstBid.contains(cfirstbid))) {
        alFirstBid.add(cfirstbid);
      }
    }

    StringBuffer sql = new StringBuffer(" select distinct cfirstbillbid,vfirstbillcode,csourcetype from ic_general_b b ,ic_general_h h where  b.cgeneralhid=h.cgeneralhid and h.dr=0 and b.dr=0 and h.cbilltypecode=? ");

    sql.append(GeneralSqlString.formInSQL("cfirstbillbid", alFirstBid));

    sql.append(" order by cfirstbillbid");

    Connection con = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try
    {
      con = getConnection();
      stmt = con.prepareStatement(sql.toString());
      stmt.setString(1, cbilltype);

      rs = stmt.executeQuery();
      HashMap ht = new HashMap();
      while (rs.next()) {
        String s = rs.getString(1);
        String code = rs.getString(2);
        String cc = rs.getString(3);
        System.out.println("rs1="+s);
        System.out.println("rs2="+code);
        System.out.println("rs3="+cc);
        if ((s != null) && (ht.containsKey(s)) && 
          (code != null) && (!alcode.contains(code))) {
          alcode.add(code);
        }

        ht.put(s, s);
      }
    }
    catch (Exception e) {
    }
    finally {
      try {
        if (stmt != null)
          stmt.close();
      } catch (Exception e) {
      }
      try {
        if (con != null)
          con.close();
      }
      catch (Exception e)
      {
      }
    }
    if (alcode.size() > 0) {
      StringBuffer sMsg = new StringBuffer(NCLangResOnserver.getInstance().getStrByID("4008busi", "UPP4008busi-000099"));
      if (voBill.isFromSource("4Y")) {
        sMsg.append(NCLangResOnserver.getInstance().getStrByID("4008busi", "UPP4008busi-000100"));
      }
      else {
        sMsg.append(NCLangResOnserver.getInstance().getStrByID("4008busi", "UPP4008busi-000101"));
      }

      for (int i = 0; i < alcode.size(); ++i) {
        sMsg.append(alcode.get(i) + "\n");
      }

      throw new BusinessException(sMsg.toString());
    }
  }

  public UFDouble getBcurrMny(String ccurtype, UFDouble nassrate, UFDouble nlocrate, UFDouble nmny, String sLogDate, BcurrArithDMO currDmo)
    throws BusinessException
  {
    try
    {
      if ((ccurtype == null) || (nmny == null) || (currDmo == null)) {
        return null;
      }

      UFDouble ufmny = null;

      if (sLogDate == null) {
        sLogDate = new UFDate(System.currentTimeMillis()).toString();
      }
      ufmny = currDmo.getAmountByKey(0, ccurtype, nmny, nassrate, nlocrate, sLogDate);

      if (ufmny == null)
        throw new BusinessException(NCLangResOnserver.getInstance().getStrByID("4008busi", "UPP4008busi-000090"));
      return ufmny;
    }
    catch (Exception e)
    {
      throw new BusinessException(e.getMessage());
    }
  }

  public GeneralBillItemVO[] queryItemData(String[] saBillPK)
    throws SQLException
  {
    GeneralBillItemVO[] voaItem = super.queryItemData(saBillPK);
    try
    {
      SettlementDMO dmo = new SettlementDMO();
      dmo.addBB3Data(voaItem, saBillPK);
    } catch (Exception e) {
      throw new SQLException(e.getMessage());
    }
    return voaItem;
  }

  public GeneralBillItemVO[] queryPureItemData(String[] saBillPK)
    throws BusinessException
  {
    GeneralBillItemVO[] voaItem = super.queryPureItemData(saBillPK);
    try
    {
      SettlementDMO dmo = new SettlementDMO();
      dmo.addBB3Data(voaItem, saBillPK);
    } catch (Exception e) {
      if (e instanceof BusinessException) {
        throw ((BusinessException)e);
      }
      throw new BusinessException("Caused by:", e);
    }
    return voaItem;
  }

  public BillVO[] setBcurrPrice(BillVO[] vos, String sLogDate)
    throws BusinessException
  {
    try
    {
      BcurrArithDMO currDmo = null;
      BcurrArithDMO currOutDmo = null;

      if ((vos == null) || (vos.length == 0)) {
        return vos;
      }
      String sInloccurpk = null;
      String sOutloccurpk = null;
      BillItemVO[] voItems = null;
      UFDouble ufmny = null;
      UFDouble ufnum = null;

      UFDouble ZERO = new UFDouble(0);
      UFDouble nexchangeotoarate = null; UFDouble nexchangeotobrate = null;
      for (int i = 0; i < vos.length; ++i)
      {
        if (vos[i] != null) {
          currDmo = new BcurrArithDMO(vos[i].getHeaderVO().getCincorpid());
          sInloccurpk = currDmo.getLocalCurrPK();
          currOutDmo = new BcurrArithDMO(vos[i].getHeaderVO().getCoutcorpid());
          sOutloccurpk = currOutDmo.getLocalCurrPK();

          voItems = vos[i].getItemVOs();
          if (voItems != null) {
            for (int j = 0; j < voItems.length; ++j) {
              if (voItems[j].getNnotaxmny() != null) {
                nexchangeotoarate = vos[i].getHeaderVO().getNexchangeotoarate();
                nexchangeotobrate = vos[i].getHeaderVO().getNexchangeotobrate();
                if ((sInloccurpk == null) || (sOutloccurpk == null) || (!sInloccurpk.equals(sOutloccurpk))) {
                  nexchangeotoarate = null;
                  nexchangeotobrate = null;
                }

                ufmny = getBcurrMny(vos[i].getHeaderVO().getCoutcurrtype(), nexchangeotoarate, nexchangeotobrate, voItems[j].getNnotaxmny(), sLogDate, currDmo);

                ufnum = voItems[j].getNnum();
                if (ufmny == null)
                  throw new BusinessException(NCLangResOnserver.getInstance().getStrByID("4008busi", "UPP4008busi-000090"));
                if ((ufmny != null) && (ufnum != null) && (ufnum.compareTo(ZERO) != 0))
                  voItems[j].setNnotaxprice(ufmny.div(ufnum).abs());
                if ((voItems[j].getFlargess() != null) && (voItems[j].getFlargess().booleanValue()))
                  voItems[j].setNnotaxprice(null);
              }
            }
          }
        }
      }
    }
    catch (Exception e)
    {
      throw new BusinessException(e.getMessage());
    }

    return vos;
  }

  public HashMap getBcurrPriceFromOut(GeneralBillVO voOut)
    throws BusinessException
  {
    HashMap hmPrice = new HashMap();
    try
    {
      if (voOut == null)
        return hmPrice;
      GeneralBillItemVO[] voItems = null;
      UFDouble ufmny = null;
      UFDouble ufnum = null;

      UFDouble ZERO = new UFDouble(0);

      BcurrArithDMO currDmoOut = new BcurrArithDMO(voOut.getHeaderVO().getPk_corp());
      BcurrArithDMO currDmoIn = new BcurrArithDMO((String)voOut.getHeaderVO().getAttributeValue("cothercorpid"));
      String sLogDate = voOut.getHeaderVO().getClogdatenow();
      if (sLogDate == null) {
        sLogDate = voOut.getHeaderVO().getDbilldate().toString();
      }
      voItems = voOut.getItemVOs();
      if (voItems != null)
      {
        for (int j = 0; j < voItems.length; ++j) {
          if (voItems[j].getNmny() == null)
            continue;
          ufmny = currDmoIn.getAmountByKey(0, currDmoOut.getLocalCurrPK(), voItems[j].getNmny(), null, null, sLogDate);

          ufnum = voItems[j].getNoutnum();
          if (ufnum == null)
            ufnum = voItems[j].getNinnum();
          if (ufmny == null)
            throw new BusinessException(NCLangResOnserver.getInstance().getStrByID("4008busi", "UPP4008busi-000090"));
          if ((ufmny != null) && (ufnum != null) && (ufnum.compareTo(ZERO) != 0))
          {
            hmPrice.put(voItems[j].getCgeneralbid(), ufmny.div(ufnum).abs());
          }
          if ((voItems[j].getFlargess() != null) && (voItems[j].getFlargess().booleanValue())) {
            hmPrice.remove(voItems[j].getCgeneralbid());
          }
        }

      }

    }
    catch (Exception e)
    {
      throw new BusinessException(e.getMessage());
    }

    return hmPrice;
  }
}